//
//  AppDelegate.m
//  XhlTrip
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "TravelViewController.h"
#import "ToolViewController.h"
#import "StrategyViewController.h"
#import "MapToolViewController.h"
#import "UMSocial.h"


@interface AppDelegate ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UIImageView *backImage;
@property (nonatomic, assign) CGFloat a;
@property (nonatomic, retain) UIView *downView;
@property (nonatomic, retain) UIView *goView;
@property (nonatomic, retain) UIImageView *jump;
@property (nonatomic, retain) UIView *bigView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    TravelViewController *travel = [[TravelViewController alloc] init];
    ToolViewController *tool = [[ToolViewController alloc] init];
    StrategyViewController *strategy = [[StrategyViewController alloc] init];
    MapToolViewController *menu = [[MapToolViewController alloc]init];
    
    travel.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"游记" image:[self nameWithPath:@"travel.png"] selectedImage:[self nameWithPath:@"travelS.png"]];
    
    strategy.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"攻略" image:[self nameWithPath:@"gonglve.png"] selectedImage:[self nameWithPath:@"gonglveS.png"]];
    
    tool.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"工具" image:[self nameWithPath:@"tool.png"] selectedImage:[self nameWithPath:@"toolS.png"]];
    
    menu.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"菜单" image:[UIImage imageNamed:@"caidan.png"] tag:1];
    
    UINavigationController *travelNavi = [[UINavigationController alloc] initWithRootViewController:travel];
    UINavigationController *toolNavi = [[UINavigationController alloc] initWithRootViewController:tool];
    UINavigationController *strategyNavi = [[UINavigationController alloc] initWithRootViewController:strategy];
    UINavigationController *menuTool = [[UINavigationController alloc]initWithRootViewController:menu];
    menuTool.navigationBarHidden = YES;
    
    
    
    NSArray *naviArr = [NSArray arrayWithObjects:travelNavi, strategyNavi, toolNavi,menuTool, nil];
    UITabBarController *tab = [[UITabBarController alloc] init];
    
    tab.tabBar.barTintColor = [UIColor whiteColor];
    tab.viewControllers = naviArr;
    self.window.rootViewController = tab;
    
    [tab release];
    
    [menu release];
    [menuTool release];
    [travel release];
    [strategy release];
    [tool release];
    [toolNavi release];
    [strategyNavi release];
    [travelNavi release];
    
    
    self.arr = [NSMutableArray arrayWithObjects:[self nameWithPath:@"go3.jpg"], [self nameWithPath:@"go2.png"], [self nameWithPath:@"go1.jpg"], [self nameWithPath:@"go4.jpg"], nil];
    
    
#pragma mark - 判断是否为第一次进入
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    if([userDefaults objectForKey:@"FirstLoad"] == nil) {
        [userDefaults setBool:NO forKey:@"FirstLoad"];
        
        
        
        
        
        self.bigView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self.bigView.backgroundColor = [UIColor whiteColor];
        [self.window addSubview:_bigView];
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(Action:) userInfo:nil repeats:YES];
        
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.itemSize = CGSizeMake(self.window.frame.size.width, self.window.frame.size.height);
        
        self.collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flow];
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flow.minimumLineSpacing = 0;
        self.collection.dataSource = self;
        self.collection.delegate = self;
        self.collection.pagingEnabled = YES;
        [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        
        
        self.backImage = [[UIImageView alloc]initWithFrame:CGRectMake(25 * Width, 0, self.window.frame.size.width - 50 * Width, self.window.frame.size.height - 200 * Height)];
        self.backImage.image = [UIImage imageNamed:@"go3.jpg"];
        [self.collection setBackgroundView:self.backImage];
        [self.bigView addSubview:_collection];
        
        
        
        self.downView = [[UIView alloc]initWithFrame:CGRectMake(0, self.window.frame.size.height - 250 * Height, self.window.frame.size.width,250 * Height)];
        self.downView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.3];
        [self.window addSubview:_downView];
        
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(self.downView.frame.size.width / 2 - 200 * Width, 50 * Height, 400 * Width , 50 * Height)];
        title.textColor = [UIColor whiteColor];
        title.text = @"GO旅行";
        title.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:30 * Width];
        
        title.textAlignment = 1;
        [self.downView addSubview:title];
        
        
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0 , 100 * Height , 375 * Width, 50 * Height)];
        lable.text = @"美景不仅仅是目的地，还应该是在路上";
        lable.textColor = [UIColor whiteColor];
        lable.textAlignment = 1;
        lable.font = [UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:20 *Width];
        [self.downView  addSubview:lable];
        
        
        
        UIButton *continueButton = [UIButton buttonWithType:UIButtonTypeSystem];
        continueButton.frame  = CGRectMake(self.window.frame.size.width / 2 - 150 * Width, 150 * Height, 300 * Width, 50 * Height);
        [continueButton setTitle:@"继续" forState:UIControlStateNormal];
        [continueButton addTarget:self action:@selector(continueButton:) forControlEvents:UIControlEventTouchUpInside];
        continueButton.backgroundColor = [UIColor redColor];
        continueButton.tintColor = [UIColor whiteColor];
        [self.downView addSubview:continueButton];
        
        self.goView = [[UIView alloc]initWithFrame:CGRectMake(self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height)];
        self.goView.backgroundColor = [UIColor whiteColor];
        
        [self.window addSubview:_goView];
        
        
        NSArray *imageArr = [NSArray arrayWithObjects:@"11111.jpg",@"22222.jpg",@"33333.jpg",@"44444.jpg",@"55555.jpg",@"66666.jpg", nil];
        
        for (int i = 0; i < 6; i++) {
            
            
            UIImageView *jump = [[UIImageView alloc]init];
            jump.backgroundColor = [UIColor redColor];
            
            jump.frame = CGRectMake(i % 2 * (150 * Width) + (100 * Width), i / 2 * (150 * Height) + (150 * Height), 0, 0);
            jump.tag = i + 1;
            jump.alpha = 0;
            
            jump.image = [UIImage imageNamed:[imageArr objectAtIndex:i]];
            [self.goView addSubview:jump];
            
            
            
            
        }
        
    }
    /**
     *  设置友盟
     */
    [UMSocialData setAppKey:@"55e3f7f967e58ee85b00110d"];
    
    
    
    
    [_window release];
    return YES;
}
- (void)continueButton:(UIButton *)button
{
    
    
    [UIView animateWithDuration:0.5 animations:^{
        
        
        self.collection.frame = CGRectMake(- self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height);
        self.downView.frame = CGRectMake(- self.window.frame.size.width, self.window.frame.size.height - 250 * Height, self.window.frame.size.width, 250 * Height);
        self.goView.frame = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height);
    }];
    
    
    
    for ( int i  = 0; i < 6; i++) {
        UIView *view  = [self.goView viewWithTag:i + 1];
        
        CGFloat j = 0;
        if (i  == 0) {
            j = 0.2;
        }
        if (i == 1) {
            j = 0.5;
        }
        if (i  == 2) {
            j = 0.8;
        }
        if (i == 3) {
            j = 1.1;
        }
        if (i == 4) {
            j = 1.4;
        }
        if (i == 5) {
            j = 1.7;
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(j * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
            [UIView animateWithDuration:0.2 animations:^{
                
                view.frame = CGRectMake(i % 2 * (185 * Width) + (20 * Height), i / 2 * (165*Width) + (90  * Height), 180 * Width, 160 * Height);
                
            }completion:^(BOOL finished) {
                
                view.frame = CGRectMake(i % 2 * (165 * Width) + (20 * Width ), i / 2 *  (155 * Height) + (90 * Height), 160 * Width, 150 * Height);
                
            }];
            
            
            [UIView animateWithDuration:1 animations:^{
                
                
                view.alpha = 1;
            }];
            
            
            
            
        });
        
        
    }
    
    
    
    
    
    NSArray *familys = [UIFont familyNames];
    
    for (int i = 0; i < [familys count]; i++)
    {
        NSString *family = [familys objectAtIndex:i];
        NSLog(@"=====Fontfamily:%@", family);
        NSArray *fonts = [UIFont fontNamesForFamilyName:family];
        for(int j = 0; j < [fonts count]; j++)
        {
            NSLog(@"***FontName:%@", [fonts objectAtIndex:j]);
        }
    }
    
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10 * Width, 10 * Height, 50 * Width, 50 * Height);
    [backButton setTintColor:[UIColor redColor]];
    [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"houtui.png"] forState:UIControlStateNormal];
    [self.goView addSubview:backButton];
    
    UILabel *detali = [[UILabel alloc]initWithFrame:CGRectMake(25 * Width, 20 * Height,self.goView.frame.size.width - 50 * Width , 100 * Height)];
    detali.text = @"嗨, 你想去哪里旅行";
    detali.font = [UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:20 * Width];
    detali.textColor = [UIColor grayColor];
    [self.goView addSubview:detali];
    
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, self.goView.frame.size.height - 150 * Height, self.goView.frame.size.width, 150 * Height)];
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 0;
    [self.goView addSubview:view];
    
    UIButton *goinButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    goinButton.frame = CGRectMake(25 * Width, self.goView.frame.size.height - 100 * Height, 320 * Width, 50 * Height);
    goinButton.backgroundColor = [UIColor redColor];
    [goinButton addTarget:self action:@selector(goin:) forControlEvents:UIControlEventTouchUpInside];
    [goinButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [goinButton setTitle:@"进入" forState:UIControlStateNormal];
    [self.goView addSubview:goinButton];
    
    
    [UIView animateWithDuration:1 animations:^{
        
        view.alpha = 0.5;
    }];
    
    
    
}


- (UICollectionViewCell  *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    return cell;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    self.backImage.image = [self.arr objectAtIndex:scrollView.contentOffset.x / self.window.frame.size.width] ;
    [self.collection setBackgroundView:_backImage];
    [self transitionWithType:kCATransitionFade WithSubtype:kCATransitionFromLeft ForView:self.window];
}
- (void)goin:(UIButton *)button
{
    
    [UIView animateWithDuration:1 animations:^{
        
        
        self.goView.alpha = 0;
    }completion:^(BOOL finished) {
        
        
        [self.collection removeFromSuperview];
        [self.downView removeFromSuperview];
        [self.goView removeFromSuperview];
        [self.bigView removeFromSuperview];
    }];
    
    
}
- (void)back:(UIButton *)button
{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        
        self.collection.frame = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height);
        self.downView.frame = CGRectMake(0, self.window.frame.size.height - 250 * Height, self.window.frame.size.width, 250 * Height);
        self.goView.frame = CGRectMake(self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height);
    }];
    
    
    for ( int i  = 0; i < 6; i++) {
        UIView *view  = [self.goView viewWithTag:i + 1];
        
        
        view.frame = CGRectMake(i % 2 * (150 * Width) + (100 * Width), i / 2 * (150 * Height) + (150 * Height), 0, 0);
        
        view.alpha = 0;
        
        
        
    }
    
    
    
    
}
- (void)Action:(NSTimer *)timer
{
    
    [self transitionWithType:kCATransitionFade WithSubtype:kCATransitionFromLeft ForView:self.window];
    
    
    self.backImage.image = [self.arr objectAtIndex:self.a];
    self.a ++;
    
    if (self.a == 4) {
        self.a = 0;
    }
    
}
- (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) view
{
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = 0.6;
    
    //设置运动type
    animation.type = type;
    if (subtype != nil) {
        
        //设置子类
        animation.subtype = subtype;
    }
    
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    
    [view.layer addAnimation:animation forKey:@"animation"];
    
}

- (UIImage *)nameWithPath:(NSString *)paths
{
    NSString *path = [[NSBundle mainBundle] pathForResource:paths ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}

- (void)dealloc
{
    [_arr release];
    [_collection release];
    [_downView release];
    [_goView release];
    [_bigView release];
    [_window release];
    [super dealloc];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
